package com.mylib.libcore.adapter;

import android.content.Context;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.mylib.libcore.R;
import com.mylib.libcore.glide.GlideApp;
import com.mylib.widget.transformer.ImgCornerTransform;


import java.util.List;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * Created by Administrator on 2018/11/28.
 * banner的适配器
 */

public class BannerPagerAdapter extends PagerAdapter {
    private List<String> mList;
    private Context mContext;
    private int defaultImg= R.drawable.ic_zhanwei_potential_img;//默认图片
    private int mRoundCorners=-1;
    private int radius=5;

    /**
     * 默认
     * @param defaultImg
     */
    public void setDefaultImg(int defaultImg) {
        this.defaultImg = defaultImg;
    }

    /**
     * 设置圆角
     * @param mRoundCorners
     */
    public void setmRoundCorners(int mRoundCorners) {
        this.mRoundCorners = mRoundCorners;
    }

    /**
     * 点击回调
     */
    public static interface OnClickImagesListener {
        void onImagesClick(int position);
    }
    private OnClickImagesListener mImagesListener;

    public void setOnClickImagesListener(OnClickImagesListener listener) {
        mImagesListener = listener;

    }

    public BannerPagerAdapter(List<String> list, Context context,int radius){
        this.mList = list;
        this.mContext = context;
        this.radius= radius;
    }
    @Override
    public int getCount() {
        return 500000;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.banner_img_layout,container,false);
        ImageView imageView = (ImageView) view.findViewById(R.id.img);

        final int index=position % mList.size();
        LoadImage(mList.get(index),imageView);
        //OnClick
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImagesListener.onImagesClick(index);
            }
        });

        container.addView(view);
        return view;
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    /**
     * 加载图片
     */
    public  void LoadImage(String url, ImageView imageview) {
        if(mRoundCorners==-1){
            GlideApp.with(mContext)
                    .asBitmap()
                    .load(url)
                    .skipMemoryCache(true)
                    //设置等待时的图片【这个时候需要注释，否则这个会作为背景图】
                    .placeholder(R.drawable.ic_zhanwei_message)
                    .error(R.drawable.ic_zhanwei_message)
                    //设置加载失败后的图片显示
                    .centerCrop()
                    //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                    .skipMemoryCache(false)
                    //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    //设置图片加载的优先级
                    .priority(Priority.HIGH)
                    .into(imageview);
        }
        else {
            ImgCornerTransform transformation = new ImgCornerTransform(mContext, dip2px(mContext, radius));
//只是绘制左上角和右上角圆角
            transformation.setExceptCorner(false, false, false, false);
            GlideApp.with(mContext)
                    .asBitmap()
                    .load(url)
                    .skipMemoryCache(true)
                    //设置等待时的图片【这个时候需要注释，否则这个会作为背景图】
                    .placeholder(R.drawable.button_line_circle_solid_selector_5)
                    .error(R.drawable.button_line_circle_solid_selector_5)
                    //设置等待时的图片【这个时候需要注释，否则这个会作为背景图】
                    //.placeholder(R.drawable.img_loading)
                    //设置加载失败后的图片显示
                    .centerCrop()
                    //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                    .skipMemoryCache(false)
                    //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    //设置图片加载的优先级
                    .priority(Priority.HIGH)
                    .transform(transformation)
                    .into(imageview);
        }
    }


}